﻿//TODO: check filter, SkinFilter3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// SkinFilter3
    /// </summary>
    public class SkinFilter3 : BaseForeBackgroundColorFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SkinFilter3"/> class.
        /// </summary>
        public SkinFilter3()
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;
            Color colorToSet = Color.Transparent;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            Color color = Color.Transparent;
            for (y = 0; y < nHeight; y++)
            {
                for ( x = 0; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    colorToSet = IsSkin(color) ? m_ColorForeground : m_ColorBackground;
                    bitmap.SetPixel(x, y, colorToSet);
                }
            }

            return _bitmap;
        }

        /// <summary>
        /// Determines whether the specified color is skin.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <returns>
        /// 	<c>true</c> if the specified color is skin; otherwise, <c>false</c>.
        /// </returns>
        public bool IsSkin(Color color)
        {
            return ((((((double)color.R) / ((double)color.B)) > 1.185) & ((((double)(color.R * color.B)) / ((double)(ColorFunctions.RGBChannelSum(color) * ColorFunctions.RGBChannelSum(color)))) > 0.107)) & ((((double)(color.R * color.G)) / ((double)(ColorFunctions.RGBChannelSum(color) * ColorFunctions.RGBChannelSum(color)))) > 0.112));
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new SkinFilter3();
        }
    }
}
